package blocstac.editimage.base
{
	import blocstac.util.Print;
	
	import flash.geom.Point;

public class SizeProperties
{
	private var _viewModel:ViewModel
	
	public function SizeProperties(viewModel:ViewModel) {
		_viewModel = viewModel
	}
	
	public function get imageIsWiderThanComponent():Boolean {
		return _viewModel.imageIsWiderThanComponent
	}
		
	public function isOnImage(componentSpace:Point):Boolean {
		var imageSpace:Point = _viewModel.componentView2imageSpace(componentSpace)
		/*trace("ViewModel.isOnImage:",
		"componentSpace:"+Print.point(componentSpace),
		"imageSpace:"+Print.point(imageSpace), 
		"imageWidthUnscaled:"+Print.trunc(_viewModel.imageWidthUnscaled), "imageHeightUnscaled:"+Print.trunc(_viewModel.imageHeightUnscaled)
		)*/
		return imageSpace.x >= 0 && imageSpace.y >= 0 && 
			imageSpace.x <= _viewModel.imageWidthUnscaled && imageSpace.y <= _viewModel.imageHeightUnscaled
	}
	
	public function component2imageSpace(componentSpace:Point):Point {
		var imageSpace:Point = _viewModel.component2imageSpace(componentSpace)
		if (imageSpace.x < 0) {
			imageSpace.x = 0
		} else if (imageSpace.x > _viewModel.imageWidthUnscaled) {
			imageSpace.x = _viewModel.imageWidthUnscaled
		}
		if (imageSpace.y < 0) {
			imageSpace.y = 0
		} else if (imageSpace.y > _viewModel.imageHeightUnscaled) {
			imageSpace.y = _viewModel.imageHeightUnscaled
		}
		return imageSpace
	}

	public function image2componentSpace(imageSpace:Point):Point {
		var componentSpace:Point = _viewModel.image2componentSpace(imageSpace)
		var imageSpaceView:Point = _viewModel.componentView2imageSpace(componentSpace)
		/*trace("SizeProperties.image2componentSpace:",
			"imageSpace:"+Print.point(imageSpace), 
			"componentSpace:"+Print.point(componentSpace),
			"imageSpaceView:"+Print.point(imageSpaceView), 
			"imageWidthUnscaled:"+Print.trunc(_viewModel.imageWidthUnscaled), "imageHeightUnscaled:"+Print.trunc(_viewModel.imageHeightUnscaled)
		)*/
		var outsideImageView:Boolean
		if (imageSpaceView.x < 0) {
			imageSpaceView.x = 0
			outsideImageView = true
		} else if (imageSpaceView.x > _viewModel.imageWidthUnscaled) {
			imageSpaceView.x = _viewModel.imageWidthUnscaled
			outsideImageView = true
		}
		if (imageSpaceView.y < 0) {
			imageSpaceView.y = 0
			outsideImageView = true
		} else if (imageSpaceView.y > _viewModel.imageHeightUnscaled) {
			imageSpaceView.y = _viewModel.imageHeightUnscaled
			outsideImageView = true
		}
		if (outsideImageView) {
			componentSpace = _viewModel.image2componentViewSpace(imageSpaceView)
			/*trace("SizeProperties.image2componentSpace cap:",
				"imageSpaceView:"+Print.point(imageSpaceView),
				"componentSpace:"+Print.point(componentSpace)
			)*/
		}
		return componentSpace
	}
	
}
}